from app.extension.database.sqlalchemy import sqla_db as db
from datetime import datetime
from .base_model import BaseModel


class ImageModel(db.Model, BaseModel):

    """商品图片模型"""
    __tablename__ = 'tb_image'
    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.String(100), comment='图片的相对地址')
    form = db.Column(db.Integer, comment='图片来源 1本地服务器 2地址是绝对地址')
    create_time = db.Column(db.DateTime, default=datetime.now)
    delete_time = db.Column(db.DateTime)

    default_hidden = ['create_time', 'delete_time']

    def __init__(self, url, form):
        self.url = url
        self.form = form

    @staticmethod
    def check_image(ids: list):
        """检查image id是否存在数据库"""
        count = ImageModel.query.filter(ImageModel.id.in_(ids)).count()
        print('check_image cuont:', count)
        return count == len(set(ids))